package com.gitee.cui.springcloud.controller;

import java.util.concurrent.TimeUnit;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 流控控制器
 *
 * @author cuiqingqiang
 * @version 1.0
 * @date 2022-03-18 15:39
 **/
@RestController
@Slf4j
public class FlowLimitController {

	@GetMapping(value = "/testA")
	public String testA() {
		return "This is testA";
	}

	@GetMapping(value = "/testB")
	public String testB() {
		return "This is testB";
	}

	@GetMapping(value = "/testC")
	public String testC() {
		log.info("---------测试RT，平均响应时间");
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return "----------测试RT，平均响应时间";
	}
	@GetMapping(value = "/testD")
	public String testD() {
		log.info("---------测试异常比例");
		int age = 10 / 0;
		return "----------测试异常比例";
	}
	@GetMapping(value = "/testE")
	public String testE() {
		log.info("---------测试异常数");
		int age = 10 / 0;
		return "----------测试异常数";
	}

	@GetMapping("/hotKey")
	@SentinelResource(value = "hotKey", blockHandler = "hotKeyFallback")
	public String testHotKey(@RequestParam(name = "p1", required = false) String p1,
	                         @RequestParam(name = "p2", required = false) String p2) {
		return "-------------testHotKey";
	}

	public String hotKeyFallback(String p1, String p2, BlockException e) {
		return "-----------hotKeyFallback";
	}
}
